import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter/cupertino.dart';

import '../resources/resources.dart';
import 'my_image.dart';
import 'my_inkwell.dart';
import 'my_text.dart';

typedef MyItemTagTapCallback = void Function(dynamic tag);

enum MyItemType {
  next, //进入下一步
  options, //选项
  unfold, //展开
  sw, //开关
  empty, //空的
  custom, //自定义
}

enum DividingLineType {
  solid,
  dotted,
}

class MyItem extends StatelessWidget {
  const MyItem({
    Key? key,
    this.onTap,
    this.onTagTap,
    required this.title,
    this.tag,
    this.isOn = false,
    this.leftWidget,
    this.titleWidget,
    this.trailingWidget,
    this.subtitle,
    this.trailingText,
    this.type = MyItemType.next,
    this.dividingLine = true,
    this.i18n = false,
    this.dividingLineType = DividingLineType.solid,
    this.height = 60,
    this.color,
  }) : super(key: key);

  final MyItemTagTapCallback? onTagTap;
  final VoidCallback? onTap;
  final String title;
  final String? subtitle;
  final String? trailingText;
  final dynamic tag;
  final bool isOn;
  final Widget? leftWidget;
  final Widget? trailingWidget;
  final Widget? titleWidget;
  final MyItemType type;
  final bool dividingLine;
  final bool i18n;
  final DividingLineType dividingLineType;
  final double height;
  final Color? color;

  Widget trailing() {
    if (trailingWidget != null) {
      return trailingWidget!;
    }
    if (type == MyItemType.next) {
      return MyPackageImage(
        "assets/img/item_chevron_right.png",
        height: 14,
        color: JmColors.textSubColor,
      );
    } else if (type == MyItemType.options) {
      if (isOn) {
        return MyPackageImage(
          "assets/img/item_selected.png",
          height: 20,
          color: JmColors.main,
        );
      } else {
        // return loadAssetImage(
        //   "assets/img/item_unselected.png",
        //   height: 20,
        //   color: JmColors.main,
        // );
        return Container();
      }
    } else if (type == MyItemType.unfold) {
      return Icon(
        FluentIcons.more,
        color: Colors.grey[400],
      );
    } else if (type == MyItemType.sw) {
      return CupertinoSwitch(
        value: isOn,
        onChanged: (_) {
          if (onTap != null) {
            onTap!();
          }
          if (onTagTap != null) {
            onTagTap!(tag);
          }
        },
      );
    } else if (type == MyItemType.custom) {
      if (trailingWidget != null) {
        return trailingWidget!;
      }
    }

    return Container();
  }

  @override
  Widget build(BuildContext context) {
    return MyInkWell(
      animation: MyInkWellAnimation.ink,
      onTap: onTap != null || onTagTap != null
          ? () {
        if (onTap != null) {
          onTap!();
        }
        if (onTagTap != null) {
          onTagTap!(tag);
        }
      }
          : null,
      child: Container(
        margin:
        const EdgeInsets.only(top: 0, left: 16.0, bottom: 1, right: 16),
        padding: const EdgeInsets.fromLTRB(0, 14.0, 0.0, 14.0),
        constraints:
        BoxConstraints(maxHeight: double.infinity, minHeight: height),
        width: double.infinity,
        decoration: BoxDecoration(
          border: dividingLine
              ?  Border(
            bottom: BorderSide(color: JmColors.line,width: 0.6),
          )
              : null,
        ),
        child: Row(
          children: <Widget>[
            leftWidget != null
                ? Container(
              padding: const EdgeInsets.only(right: 10),
              child: leftWidget,
            )
                : Container(),
            Expanded(
              child: subtitle != null
                  ? Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  titleWidget ??
                      MyText(
                        title,
                        size: 16,
                        //color: Color(0xFF666666),
                        textAlign: TextAlign.start,
                        i18n: i18n,
                      ),
                  const SizedBox(height: 1.0),
                  MyText(
                    subtitle!,
                    size: 14,
                    color: Colors.grey[500],
                    textAlign: TextAlign.start,
                  ),
                ],
              )
                  : titleWidget ??
                  MyText(
                    title,
                    size: 16,
                    //color: Color(0xFF666666),
                    textAlign: TextAlign.start,
                    i18n: i18n,
                  ),
            ),
            const SizedBox(width: 10.0),
            trailingText != null
                ? Container(
              padding: const EdgeInsets.only(right: 4),
              child: MyText(
                trailingText!,
                size: 14,
                color: JmColors.textSubColor,
                textAlign: TextAlign.start,
              ),
            )
                : Container(),
            //Spacer(),
            trailing(),
          ],
        ),
      ),
    );
  }
}
